package com.alibaba.org.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.MD5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.org.service.IEmployeeService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class   EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void add(Employee employee) {
        //加密
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密后的密码，存入logininfo
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //保存logininfo信息
        //将employee和logininfo表中相同字段赋值
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.add(logininfo);

        //设置employee的logininfo_id
        //获取logininfo的id
        Long logininfoId = logininfo.getId();
        //将logininfo的id值关联employee的logininfoId
        employee.setLogininfo_id(logininfoId);
        //将内存中的对象employee添加到数据库
        employeeMapper.add(employee);
        //判断如果角色ID不为空 添加角色与员工中间表
        if (employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }

    }

    @Override
    public void delete(Long id) {
        Employee employee = employeeMapper.findOne(id);
        //删除登录信息表数据
        logininfoMapper.delete(employee.getLogininfo_id());

        //删除中间表数据
        employeeMapper.deleteEmployeeRole(id);
        //删除employee表数据
        employeeMapper.delete(id);
    }

    @Override
    public void update(Employee employee) {
        //处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        Long logininfo_id = employeeMapper.findOne(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);

        //删除员工角色
        employeeMapper.deleteEmployeeRole(employee.getId());
        //判断如果角色ID不为空 添加角色与员工中间表
        if (employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }

        //登录信息id和店铺信息id不修改
        employeeMapper.update(employee);
    }

    //将employee和logininfo表中相同字段赋值
    private Logininfo employee2Logininfo(Employee employee){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        //设置type 当前对象是店铺管理员
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
}
